<?php
require_once '../conn.php';
require_once '../libs/global.cls';

$sname = $_POST['txtidentity'];
$cid = $_POST['txtcid'];
$ref = $_POST['txtacc'];    //ref=acc, 则页面是从添加联系人跳转过来的

// 身份为空
if(empty ($sname)){
    $error_type = 301;
    include_once '../error/identity_error.php';
    exit;
}

// 判断数据库该用户对应的身份是否存在
$sql = "select sname from cc_identity where cc_id='". $cid ."'";
$res = $ms_cc_pdo->select($sql);
$row = $res['result'];
foreach ($row as $key=>$item){
    if($item['sname'] == $sname){
        $error_type = 303;
        $cid = $cid;
        $ref = $ref;
        include_once '../error/identity_error.php';
        exit;
    }
}

// 验证通过，添加身份
$params = array();
$params[':cc_id'] = (int)$cid;
$params[':sname'] = $sname;
$params[':ctime'] =  time();

//添加到数据库
$sql_insert = sprintf( "INSERT INTO `cc_identity` (`sname`, `cc_id`, `ctime`) VALUES ( :sname, :cc_id, :ctime )" );

$res_insert = $ms_cc_pdo->insert($sql_insert,$params);
if(!$res_insert['errorcode']){
    if($ref=='acc'){
        redirect("../contact/contact_add.php?cid=".$cid);
    }
    // todo: 其它情况跳转
}

?>
